// pages/judgecontent/judgecontent.js
Page({

  data: {
    itemid:"",
    judgelevel:[
      {
        level:'全部',
        num:"",
        choose:false
      }, {
        level:'好',
        num:"",
        choose:false
      }, {
        level:'一般',
        num:"",
        choose:false
      }, {
        level:'差',
        num:"",
        choose:false
      },
    ],
    judgecontent:[]
  },
  onLoad(options){
    var that = this;
    var value = 'judgelevel[0].choose';
    this.setData({
      [value]:true,
      itemid:options.itemid
    })

    wx.request({
      url: 'http://127.0.0.1:8080/itemJudge/selectJudgeByItemid',
      method:"POST",
      data:{itemid:options.itemid},
      success(res){
        if(res.data != ""){
          var all = 'judgelevel[0].num';
          that.setData({
            [all]:res.data.length
          })
          for(var i=0;i<res.data.length;i++){
            res.data[i].itemJudgePic = JSON.parse(res.data[i].itemJudgePic)
          }
          
          that.setData({
            judgecontent:res.data
          })
        }

        // console.log(res)
      }
    })

    //好评数
      var i =1;
      var hao = 'judgelevel['+i+'].num';
      wx.request({
        url: 'http://127.0.0.1:8080/itemJudge/selectDifferJudge',
        method:"POST",
        data:{
          itemid:options.itemid,
          rank:that.data.judgelevel[i].level
        },
        success(res){
            that.setData({
              [hao]:res.data.length
            })
        }
      })

      //一般数
      var j =2;
      var yiban  = 'judgelevel['+j+'].num';
      wx.request({
        url: 'http://127.0.0.1:8080/itemJudge/selectDifferJudge',
        method:"POST",
        data:{
          itemid:options.itemid,
          rank:that.data.judgelevel[j].level
        },
        success(res){
            that.setData({
              [yiban]:res.data.length
            })
        }
      })

      //一般数
      var k =3;
      var cha  = 'judgelevel['+k+'].num';
      wx.request({
      url: 'http://127.0.0.1:8080/itemJudge/selectDifferJudge',
      method:"POST",
      data:{
        itemid:options.itemid,
        rank:that.data.judgelevel[k].level
    },
      success(res){
        that.setData({
          [cha]:res.data.length
        })
      }
    })


    
   
  
  },

  // 选择评论等级
  choosejudge(e){
    console.log(this.data.judgelevel)
    var that = this;
    var index = e.currentTarget.dataset.index;
    var value = 'judgelevel['+index+'].choose';
    this.setData({
      [value]:true
    })
    for(var i = 0;i<this.data.judgelevel.length;i++){
      var value = 'judgelevel['+i+'].choose';
      if(i != index){
        this.setData({
          [value]:false
        })
      }
    }

    if(this.data.judgelevel[0].choose){
      wx.request({
        url: 'http://127.0.0.1:8080/itemJudge/selectJudgeByItemid',
        method:"POST",
        data:{itemid:that.data.itemid},
        success(res){
          if(res.data != ""){
            var all = 'judgelevel[0].num';
            that.setData({
              [all]:res.data.length
            })
            for(var i=0;i<res.data.length;i++){
              res.data[i].itemJudgePic = JSON.parse(res.data[i].itemJudgePic)
            }
            
            that.setData({
              judgecontent:res.data
            })
          }
  
          // console.log(res)
        }
      })
    }else{
      wx.request({
        url: 'http://127.0.0.1:8080/itemJudge/selectDifferJudge',
        method:"POST",
        data:{
          itemid:that.data.itemid,
          rank:that.data.judgelevel[index].level
      },
        success(res){
        //  console.log(res)
        if(res.data != ""){
          for(var i=0;i<res.data.length;i++){
            res.data[i].itemJudgePic = JSON.parse(res.data[i].itemJudgePic)
          }
          
          that.setData({
            judgecontent:res.data
          })
        }
    
        }
      })
    }


  },

  // 查看图片
  showpic(e){
    console.log(e)
    var firstindex = e.currentTarget.dataset.firstindex;
    var index  = e.currentTarget.dataset.index;
    console.log(index)
    console.log(this.data.judgecontent[firstindex].picarray[index])
    wx.previewImage({
      current: this.data.judgecontent[firstindex].picarray[index],     //当前图片地址
      urls: this.data.judgecontent[firstindex].picarray,                 //所有要预览的图片的地址集合 数组形式
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

})